<?php

/**
 * Description of PropertyCaseController
 *
 * @author Poh Nean
 */
class PropertyCaseController extends PsController
{

	public function accessRules()
	{
		return CMap::mergeArray(array(
					array('allow',
						'users' => array('@'),
					),
						), parent::accessRules());
	}

	public function actionSearch($term)
	{
		$criteria = new CDbCriteria();
		$criteria->select = 't.id, t.case_number, p.prop_address_formatted';
		$criteria->join = 'INNER JOIN `property` p ON (t.property_id = p.id)';
		$criteria->addCondition('(UPPER(t.case_number) LIKE :term OR p.prop_address_formatted LIKE :term)');
		$criteria->params[':term'] = "%" . $term . "%";
		$criteria->compare('t.agency_id', Yii::app()->user->model->agency_id);
		$criteria->compare('t.approval_status', '>=' . PropertyCase::APPROVAL_STATUS_NOT_SUBMITTED);
		$criteria->compare('t.deleted', 0);
		$criteria->compare('t.submitted', 1);
		$criteria->limit = 25;

		$data = Yii::app()->db->commandBuilder->createFindCommand('property_case', $criteria)->queryAll();

		$propertyCases = array();
		if (!empty($data)) {
			foreach ($data as $record) {
				$propertyCases[] = array(
					'label' => '#' . $record['case_number'] . ': ' . $record['prop_address_formatted'],
					'value' => $record['case_number'],
					'id' => $record['id'],
				);
			}
		}

		print(CJSON::encode($propertyCases));
	}

}

?>
